package com.manage.handler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class SinaUserController {
	// 数据库名称
	private static String DB_NAME = "bugmanage.db";
	// 数据库版本
	private static int DB_VERSION = 1;
	private SQLiteDatabase db;
	private SqliteSinaHelper dbSinaHelper;

	// private SqliteHelper dbHelper;

	public SinaUserController(Context context) {
		dbSinaHelper = new SqliteSinaHelper(context, DB_NAME, null, DB_VERSION);
		// dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION);
		db = dbSinaHelper.getWritableDatabase();
	}

	public void Close() {
		db.close();
		dbSinaHelper.close();
	}

	// 获取users表中的USER_ID、Access Token、Access Secret的记录
	public List<SinaUser> GetUserList(Boolean isSimple) {
		List<SinaUser> userList = new ArrayList<SinaUser>();
		Cursor cursor = db.query(SqliteSinaHelper.TB_NAME, null, null, null, null, null, SinaUser.ID + " DESC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(1) != null)) {
			SinaUser user = new SinaUser();
			user.setId(cursor.getString(0));
			user.setUserId(cursor.getString(1));
			user.setToken(cursor.getString(2));
			user.setTokenSecret(cursor.getString(3));
			if (!isSimple) {
				user.setUserName(cursor.getString(4));
				ByteArrayInputStream stream = new ByteArrayInputStream(cursor.getBlob(5));
				Bitmap bitmap = BitmapFactory.decodeStream(stream);
				// Drawable icon = Drawable.createFromStream(stream, "image");
				user.setUserIcon(bitmap);
			}
			userList.add(user);
			cursor.moveToNext();
		}
		cursor.close();
		return userList;
	}

	public boolean isNullSinaTb() {
		Boolean flag = false;
		Cursor cursor = db.query(SqliteSinaHelper.TB_NAME, null, null, null, null, null, SinaUser.ID + " DESC");
		flag = !cursor.moveToFirst();
		cursor.close();
		return flag;
	}

	// 判断users表中的是否包含某个USER_ID的记录
	public boolean HaveUserInfo(String userId) {
		Boolean b = false;
		Cursor cursor = db.query(SqliteSinaHelper.TB_NAME, null, SinaUser.USER_ID + "=" + userId, null, null, null, null);
		b = cursor.moveToFirst();
		// Log.e("HaveUserInfo", b.toString());
		cursor.close();
		return b;
	}

	// 更新users表的记录，根据USER_ID更新用户昵称和用户图标
	public int UpdateUserInfo(String userName, Bitmap userIcon, String userId) {
		ContentValues values = new ContentValues();
		values.put(SinaUser.USER_NAME, userName);
		// BLOB类型
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		// 将Bitmap压缩成PNG编码，质量为100%存储
		userIcon.compress(Bitmap.CompressFormat.PNG, 100, os);
		// 构造SQLite的Content对象，这里也可以使用raw
		values.put(SinaUser.USER_ICON, os.toByteArray());
		int id = db.update(SqliteSinaHelper.TB_NAME, values, SinaUser.USER_ID + "=" + userId, null);
		// Log.e("UpdateUserInfo2", id + "");
		return id;
	}

	// 更新users表的记录
	public int UpdateUserInfo(SinaUser user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(SinaUser.USER_ID, user.getUserId());
		values.put(SinaUser.TOKEN, user.getToken());
		values.put(SinaUser.TOKENSECRET, user.getTokenSecret());
		if (!isSimple) {
			values.put(SinaUser.USER_NAME, user.getUserName());
			Bitmap bitmap = user.getUserIcon();
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
			values.put(SinaUser.USER_ICON, os.toByteArray());
		}
		int id = db.update(SqliteSinaHelper.TB_NAME, values, SinaUser.USER_ID + "=" + user.getUserId(), null);
		// Log.e("UpdateUserInfo", id + "");
		return id;
	}

	// 添加users表的记录
	public Long SaveUserInfo(SinaUser user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(SinaUser.USER_ID, user.getUserId());
		values.put(SinaUser.TOKEN, user.getToken());
		values.put(SinaUser.TOKENSECRET, user.getTokenSecret());
		if (!isSimple) {
			values.put(SinaUser.USER_NAME, user.getUserName());
			Bitmap bitmap = user.getUserIcon();
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
			values.put(SinaUser.USER_ICON, os.toByteArray());
		}
		Long uid = db.insert(SqliteHelper.TB_NAME, SinaUser.ID, values);
		// Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 删除users表的记录
	public int DelUserInfo(String userId) {
		int id = db.delete(SqliteHelper.TB_NAME, SinaUser.USER_ID + "=" + userId, null);
		// Log.e("DelUserInfo", id + "");
		return id;
	}
}
